import * as FileSystem from 'expo-file-system';
import { Asset } from 'expo-asset';

export async function copyDatabaseFromAssets() {
  const dbName = 'books.db';
  const dbAsset: any = Asset.fromModule(require('../assets/db/books.db'));
  await dbAsset.downloadAsync();
  console.log(FileSystem.documentDirectory)
  const localUri = `${FileSystem.documentDirectory}SQLite/${dbName}`;

  // 检查文件是否已经存在
  const fileInfo = await FileSystem.getInfoAsync(localUri);
    //   if (!fileInfo.exists) {
        // 如果文件不存在，则从 assets 复制到本地
    await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}SQLite`, { intermediates: true });
    await FileSystem.copyAsync({
        from: dbAsset.localUri,
        to: localUri,
    });
    //   }
  return localUri;
}